require.config({
    baseUrl: 'js/common'
});
define(['common', 'pagin'], function(defaultStr, pagin) {
    var baseUrl = defaultStr();
    // console.log(pagin.paginGetli(4, 2));
    /*
     *------助管信息管理
     *增加助管：addZhuguan
     *删除助管：deleteZG
     *查询数据：queryData
     *向服务器获取所有数据：getAllData
     *向服务器查询数据（页）：getPage
     *获取学院信息列表，获取银行列表：getDept,getBanks，目前是写死的
     *-------说明
     *-------
     *-------
     */
    var dept_info = new Vue({
        el: '#dept-data',
        data: {
            /*查询对象*/
            query: {
                "rows": 5,
                "page": 1,
                "deptNo": ''
            },
            /*pageNum 总页数*/
            pageNum: 1,
            /*当前页码*/
            curPage: 1,
            /*pages用于构造page*/
            pages: [],
            /*部门列表*/
            depts: [],
            select_deptNo: '',
            /*列表中的项目*/
            items: [],
            /*选中删除的助管对象*/
            deleteItem: '',
            /*选中的要修改的学院信息*/
            editItem: '',
            /*要添加的助管对象*/
            addItem: '',
            url: {
                'addDept': baseUrl + '/base/dept/add.do',
                'delDept': baseUrl + '/base/dept/delete.do',
                'modifyDept': baseUrl + '/base/dept/update.do',
                'getDepts': baseUrl + '/base/dept/list.do'
            }
        },
        methods: {
            /*初始化*/
            init: function() {
                var _self = this;
                _self.queryData();
            },
            /*查询数据*/
            queryData: function() {
                var _self = this;
                _self.query.deptNo = _self.select_deptNo;
                query = JSON.stringify(_self.query);
                $.ajax({
                    type: "POST",
                    url: _self.url.getDepts,
                    data: query,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        _self.items = [];
                        _self.depts = [];
                        $.each(result.result, function(i, value) {
                            _self.items.push(value);
                            _self.depts.push(value);
                        });
                        /*更新分页*/
                        // _self.pages = pagin.paginGetli(_self.page, _self.curPage);
                        // return _self.items;
                    }
                });
            },
            /*根据row和page请求数据*/
            getDataPage: function(cur_page) {
                var _self = this;
                _self.query.page = cur_page;
                _self.curPage = cur_page;
                _self.queryData();
            },
            /*处理分页查询*/
            submitPage: function(p) {
                console.log(p);
                if (p === ">>") {
                    _self.curPage++;
                    _self.getDataPage(_self.curPage);
                } else if (p === "<<") {
                    _self.curPage--;
                    _self.getDataPage(_self.curPage);
                } else {
                    _self.curPage = p;
                    _self.getDataPage(_self.curPage);
                }
            },
            /*添加一条学院信息*/
            addDept: function() {
                var _self = this;
                var additem = JSON.stringify(_self.addItem);
                $.ajax({
                    type: "POST",
                    url: _self.url.addDept,
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: additem,
                    success: function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-add').modal('hide');
                            _self.queryData();
                        }, 1000);
                    }
                });
            },
            /*提交删除一条助管信息到modal*/
            deleteInfo: function(i, name) {
                var _self = this;
                _self.deleteItem = {
                    "deptID": i,
                    "deptName": name
                };
            },
            /*提交删除一条助管信息到服务器*/
            deleteDept: function() {
                var _self = this;
                var deleteItem = JSON.stringify({
                    "deptID": _self.deleteItem.deptID
                });
                $.ajax({
                    type: "POST",
                    url: _self.url.delDept, //
                    data: deleteItem,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-delete').modal('hide');
                            _self.queryData();
                        }, 1000);
                    }
                })
            },
            /*提交编辑一条助管信息到modal*/
            editInfo: function(i, name) {
                console.log('editInfo');
                var _self = this;
                _self.editItem = {
                    "deptID": i,
                    "deptName": name
                };
            },
            /*提交编辑到服务器*/
            editDept: function() {
                var _self = this;
                var editItem = JSON.stringify({
                    "deptID": _self.editItem.deptID,
                    "deptName": _self.editItem.deptName,
                    "jobNum": parseInt(_self.editItem.jobNum)
                });
                $.ajax({
                    type: "POST",
                    url: _self.url.modifyDept, //
                    data: editItem,
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',
                    success: function(result) {
                        alert(result.message);
                        setTimeout(function() {
                            $('#myModal-edit').modal('hide');
                            _self.queryData();
                        }, 1000);
                    }
                })
            }
        }
    });
    dept_info.init();
});
